//
//  ViewController.m
//  123
//
//  Created by Apple on 2017/7/7.
//  Copyright © 2017年 闪电极送. All rights reserved.
//

#import "ViewController.h"
#import "ButtonSwitchView.h"
#import "Masonry.h"
#import <ReactiveCocoa/ReactiveCocoa.h>


@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    NSArray *datas;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //创建一个layout布局类
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小为100*100
    layout.itemSize = CGSizeMake(100, 100);
    //创建collectionView 通过一个布局策略layout来创建
    UICollectionView * collect = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    //代理设置
    collect.delegate=self;
    collect.dataSource=self;
    //注册item类型 这里使用系统的类型
    [collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];
    [collect registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
    
    [self.view addSubview:collect];
    
//    UILabel *lab = [[UILabel alloc] init];
//    lab.frame =CGRectMake(50, 50, 30, 20);
//    [self.view addSubview:lab];
//    lab.backgroundColor = [UIColor blueColor];
//    lab.text = @"我的中ASSSSSSSSSSSSS国心CHANGIANGHAUANGHEZAIWOXINZHINGAN";
//    [lab sizeToFit];
    
    
    ButtonSwitchView *viewButton = [[ButtonSwitchView alloc] init];
    viewButton.frame = CGRectMake(0, 100,self.view.frame.size.width, 50);
    viewButton.tag = 1000;
    
    [[RACObserve(viewButton, status)skip:1] subscribeNext:^(id x)
     {
         NSLog(@"%ld",(long)[x integerValue]);
         if ([x integerValue]==0) {
             NSLog(@"热门推荐数据");
         }
         else if ([x integerValue]==1)
         {
             NSLog(@"最高收益数据");
         }
         else if ([x integerValue]==2)
         {
             NSLog(@"当日返现数据");
         }
         else if ([x integerValue]==3)
         {
             NSLog(@"风控得分数据");
         }
     }];
    [self.view addSubview:viewButton];
    
    
//    [[RACObserve(viewButton, status) skip:1] subscribeNext:^(id x) {
//        if ([x integerValue] == 0) {
////            datas = [NSArray yy_modelArrayWithClass:[MainPageDataMode class] json:[((ModulesModel *)self.dataSources).data objectForKey:@"websites"]];
//        }else{
////            datas = [NSArray yy_modelArrayWithClass:[MainPageDataMode class] json:[((ModulesModel *)self.dataSources).data objectForKey:@"brands"]];
//        }
////        isBrand = [x integerValue];
////        [UIView transitionWithView:self.cellCollectionView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
////            [self.cellCollectionView reloadData];
//        } completion:^(BOOL finished) {
//            
//        }];
//    }];
//    [self.view addSubview:viewButton];
//    [viewButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
//    }];

    
    
    
    
}
//这是正确的方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader)
    {
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader" forIndexPath:indexPath];
        headView.backgroundColor = [UIColor redColor];
        
        if (![headView viewWithTag:1000]) {
            ButtonSwitchView *viewButton = [[ButtonSwitchView alloc] init];
            viewButton.tag = 1000;
            [[RACObserve(viewButton, status) skip:1] subscribeNext:^(id x) {
                if ([x integerValue] == 0) {
                    //datas = [NSArray yy_modelArrayWithClass:[MainPageDataMode class] json:[((ModulesModel *)self.dataSources).data objectForKey:@"websites"]];
                }else{
                   // datas = [NSArray yy_modelArrayWithClass:[MainPageDataMode class] json:[((ModulesModel *)self.dataSources).data objectForKey:@"brands"]];
                }
                //isBrand = [x integerValue];
//                [UIView transitionWithView:self.cellCollectionView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
//                    [self.cellCollectionView reloadData];
//                } completion:^(BOOL finished) {
//                    
//                }];
            }];
            [headView addSubview:viewButton];
            [viewButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
            }];
        }
        
        //view.backgroundColor = [UIColor whiteColor];
        
        
        
        return headView;
    }
    return nil;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    
    
   return CGSizeMake(self.view.frame.size.width, 50);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
